import {defineStore} from 'pinia';
import {Helper, Msg, Cache, Log} from 'core';
var QQMapWX = require('../../static/libs/qqmap-wx-jssdk.js');
const store = {
    id: "map",
    state(){
        return {
            qqmapsdk: null
        }
    },

    getters: {

    },

    actions: {
        init() {
            // 实例化API核心类
            this.qqmapsdk = new QQMapWX({
                key: "XXGBZ-5TC6W-VFURW-RR6K5-PAWDT-AWFWR"
            });
        },

        geocoder(address) {
            return new Promise((resolve, reject) => {
                if(Helper.empty(this.qqmapsdk)) {
                    this.init();
                }
                this.qqmapsdk.geocoder({
                    address: address,
                    success: (res) => {
                        resolve({code: 200, data: res.result});
                    },

                    fail: function(error) {
                        resolve({code: error.status, message: error.message});
                    },
                });
            })
        },

        /**
         * 计算距离
         * latitude: 39.984060,
         * longitude: 116.307520
         * @param from
         * @param to
         */
        calculateDistance(from, to) {
            return new Promise((resolve, reject) => {
                if(Helper.empty(this.qqmapsdk)) {
                    this.init();
                }
                this.qqmapsdk.calculateDistance({
                    mode: "driving",
                    from: from,
                    to: typeof(to) == 'object' ? [to] : to,
                    success: (res) => {
                        resolve({code: 200, data: res.result});
                    },

                    fail: function(error) {
                        resolve({code: error.status, message: error.message});
                    },
                });
            })
        }
    },
}

export default defineStore(store)
